<?php

/**
 * Class User
 * @author wangXi <iwisunny@gmail.com>
 * Date: 2016/2/9
 */
class User extends Home_Controller
{
    protected $group_list=[];

    function __construct()
    {
        parent::__construct();

        if( !$this->session->userdata('is_admin') &&
            !($this->router->class=='user' && $this->router->method == 'reset_pwd')){
            redirect(base_url());
        }

        $this->load->model('user_model', 'users');
        $this->load->model('group_model', 'groups');

        $this->group_list = $this->groups->get_group_list();

    }

    function index(){
        $user_list=$this->users->get_all();
        $this->tpl->render('user/user.twig', [
            'user_list'=> $user_list,
            'group_list'=> $this->group_list
        ]);
    }


    function add()
    {
        if (is_xhr()) {
            $this->handle_ajax_add();
        }

        $this->tpl->render('user/user_add.twig', array(
            'group_list'  => $this->group_list
        ));
    }

    protected function handle_ajax_add(){
        $data = $this->input->post_get('entity', true);
        $username = $data['username'];
        $passwd = $data['password'];
        if(isset($data['group_id'])){
            $data['group_id'] = intval($data['group_id']);
        }

        if(empty($data['id'])){
            $check_user = $this->users->get_by('username', $username);
            if (!empty($check_user)) {
                $this->put_msg(['info' => '用户已存在']);
            }
        }

        $salt = substr(md5($username . time()), 0, 10);
        $data['salt'] = $salt;
        $data['password'] = md5($salt . $passwd);

        $status=false;
        $this->users->skip_validation();
        if(isset($data['id']) && empty($data['id'])){
            unset($data['id']);
            $insert_id = $this->users->insert($data);
            $status=$insert_id>0;
        }
        else{
            $status=$this->users->update($data['id'], $data);
        }

        $this->set_msg('success', $status)->put_msg();
    }

    function edit(){
        $uri_seg=$this->uri->rsegment_array();
        $user=[];
        if(isset($uri_seg[3]) && $uri_seg[3]>0){
            $user=$this->users->get($uri_seg[3]);
        }

        $this->tpl->render('user/user_add.twig', array(
            'group_list'  => $this->group_list,
            'entity'=> $user
        ));
    }

    function delete(){
        if(is_xhr()){
            $uid=intval($this->input->get('user_id'));
            $st=$this->users->delete($uid);
            $this->put_msg(['success' => $st ? true : false]);
        }

        $this->put_msg();
    }

    function reset_pwd(){
        if(is_xhr()){
            $this->handle_ajax_add();
        }

        $uname=$this->session->userdata('username');
        $user=$this->users->get_by(array('username'=>$uname));
        $this->tpl->render('user/user_add.twig', array(
            'group_list'  => $this->group_list,
            'entity'=> $user,
            'pg_name'=>'修改密码',
            'reset_pwd'=>true
        ));
    }


}

